package com.fire.util;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

import com.fire.common.RspObjBean;


public class BussinessUtils
{

	public static <T> RspObjBean rspErrorHeadInfo(String code, String desc, T clzz, int count)
	{
		RspObjBean rspObjBean = new RspObjBean();
		rspObjBean.setCode(code);
		rspObjBean.setDesc(desc);
		rspObjBean.setCount(count);

		try
		{
			Method getListInfo = clzz.getClass().getMethod("getListInfo");
			rspObjBean.setListInfo(getListInfo.invoke(clzz));

		} catch (Exception e)
		{
			rspObjBean.setInfo(clzz);
		}

		return rspObjBean;
	}

	public static <T> RspObjBean rspErrorHeadInfo(String code, String desc, T clzz)
	{
		RspObjBean rspObjBean = new RspObjBean();
		rspObjBean.setCode(code);
		rspObjBean.setDesc(desc);
		rspObjBean.setInfo(clzz);

		return rspObjBean;
	}

	public static RspObjBean rspErrorHeadInfo(String code, String desc)
	{
		RspObjBean rspObjBean = new RspObjBean();
		rspObjBean.setCode(code);
		rspObjBean.setDesc(desc);
		return rspObjBean;
	}

	public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
			"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8",
			"9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
			"U", "V", "W", "X", "Y", "Z" };

	public static String getKeyByTypeCode()
	{
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 16; i++)
		{
			String str = uuid.substring(i * 2, i * 2 + 2);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString();
	}

	public static String getKeyByTypeCode(String type)
	{
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 16; i++)
		{
			String str = uuid.substring(i * 2, i * 2 + 2);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return type + shortBuffer.toString();
	}

	/**
	 *
	 * @Description:
	 * @param startCode
	 * @param endCode
	 * @return
	 * @version:v1.0
	 * @author:PHYX
	 * @date:2015-10-24 ����09:36:42
	 */
	public static String getOrderCode()
	{
		Random jjj = new Random();
		Date currentTime = new Date();

		String key = "";
		String randomNum = "";

		SimpleDateFormat formatter = new SimpleDateFormat("yyMMddHHmmss");
		String dateString = formatter.format(currentTime);

		for (int k = 0; k < 4; k++)
		{
			randomNum = randomNum + jjj.nextInt(9);
		}

		key = dateString + randomNum;
		return key;
	}

	/**
	 *
	 * @Description:��������ͼƬ·����ȡͼƬ����·��
	 * @param picUrl
	 * @return
	 * @version:v1.0
	 * @author:PHYX
	 * @date:2015-10-24 ����09:37:10
	 */
	public static String getFinalPicUrl(String picUrl)
	{
		String picPath = "";
		String sub = "/fileUpload/upload";
		int a = picUrl.indexOf(sub);
		
		if (a >= 0)
		{
			String[] infos = picUrl.split(sub);
			picPath = infos[1];
		} else
		{
			picPath = picUrl;
		}
		return picPath;
	}

	/**
	 *
	 * @Description:������Ϣ
	 * @param xmlInfo
	 * @return
	 * @throws Exception
	 * @version:v1.0
	 * @author:PHYX
	 * @date:2015-12-7 ����4:13:35
	 */
	public static String pushHttpPost(String xmlInfo) throws Exception
	{

		// ���󷵻ؽ��
		String result = "";
		// String postUrl = StaticVariable.PUSH_URL;
		//
		// // �����ַ
		// URL url = new URL(postUrl);
		// URLConnection con = url.openConnection();
		//
		// con.setDoOutput(true);
		// con.setConnectTimeout(60 * 000);// ���ӳ�ʱ(��λ����)
		// con.setReadTimeout(60 * 000);// ��ȡ��ʱ(��λ����)
		// con.setRequestProperty("Pragma:", "no-cache");
		// con.setRequestProperty("Cache-Control", "no-cache");
		// con.setRequestProperty("Content-Type", "text/html");
		//
		// OutputStream os = con.getOutputStream();
		// os.write(xmlInfo.getBytes("UTF-8"));
		// os.flush();
		//
		// InputStream is = con.getInputStream();
		//
		// result = IOUtils.toString(is, "UTF-8");
		//
		return result;
	}
}
